<?php
/**
 * Created by PhpStorm.
 * User: long
 * Date: 2018/06/29
 * Time: 14:48
 */

namespace Advertisement\Model;

use Common\Model\CommonModel;

class BannerModel extends CommonModel
{
    const LINK_OUT      = 'out';//外链
    const LINK_NEWS     = 'news';//新闻类
    const LINK_PRODUCT  = 'product';//商品详情


    const APP_STORE = 'app-store';//商城首页
    const APP_HOME  = 'app-home';//app首页轮播图

    const TYPE_APP  = '1';//APP
    const TYPE_PC   = '2';//PC

    const STATUS_YES = '1';//启用
    const STATUS_NO  = '0';//禁用

    /**
     * @param $sign
     * @param string $type
     * @return array
     * 获取 banner
     */
    public function getBanner($sign, $type = self::TYPE_PC)
    {
        $banner_image_model = new BannerImageModel();
        //已启用的广告
        $list = $this
            ->where(array('sign_key' => $sign, 'status' => self::STATUS_YES, 'type' => $type))
            ->field('id')
            ->select();

        $ids = array_column($list, 'id');
        $ids = implode(',', $ids);
        //已启用的广告所包含的图，限5 张
        $info = $banner_image_model
            ->where(array('banner_id' =>array('in', $ids)))
            ->field('title, link, image, type')
            ->order('sort_order asc')
//            ->limit(5)
            ->select();
        $data = [];
        foreach($info as $k => $v) {
            if($v['type'] == self::LINK_NEWS) {
                $data[] = array(
                    'type' => $v['type'],
                    'link' => $v['link'],
                    'image' => setUrl($v['image']),
                    'web_url' => sp_get_host().'/Wap/Information/news/id/'.$v['link'],
                );
            } else if($v['type'] == self::LINK_OUT) {
                $data[] = array(
                    'type' => $v['type'],
                    'link' => $v['link'],
                    'image' => setUrl($v['image']),
                    'web_url' => $v['link'],
                );
            } else {
                $data[] = array(
                    'type' => $v['type'],
                    'link' => $v['link'],
                    'image' => setUrl($v['image']),
                    'web_url' => $v['link'],
                );
            }

        }
        return $data;
    }


}